import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:vista/shared/utils/flash_notifier.dart';
import 'package:vista/shared/utils/widget_extension.dart';

/// 举报原因类型
enum ComplainReason {
  pornographicVulgarity,
  hateSpeech,
  nauseating,
  illegalActivity,
  spamAdvertisement,
  otherIssues,
  blockUser,
}

/// 举报原因扩展
extension ComplainReasonExtension on ComplainReason {
  String get label {
    switch (this) {
      case ComplainReason.pornographicVulgarity:
        return 'Pornographic vulgarity';
      case ComplainReason.hateSpeech:
        return 'Hate Speech';
      case ComplainReason.nauseating:
        return 'Nauseating';
      case ComplainReason.illegalActivity:
        return 'Illegal Activity';
      case ComplainReason.spamAdvertisement:
        return 'Spam/Advertisement';
      case ComplainReason.otherIssues:
        return 'Other Issues';
      case ComplainReason.blockUser:
        return 'Block User';
    }
  }

  IconData get icon {
    switch (this) {
      case ComplainReason.pornographicVulgarity:
        return Icons.warning_rounded;
      case ComplainReason.hateSpeech:
        return Icons.cancel_rounded;
      case ComplainReason.nauseating:
        return Icons.sick_rounded;
      case ComplainReason.illegalActivity:
        return Icons.gavel_rounded;
      case ComplainReason.spamAdvertisement:
        return Icons.block_rounded;
      case ComplainReason.otherIssues:
        return Icons.more_horiz_rounded;
      case ComplainReason.blockUser:
        return Icons.person_off_rounded;
    }
  }
}

/// 举报底部表单
/// 用于显示举报选项，用户可以选择举报原因
class ComplainBottomSheet extends StatelessWidget {
  /// 举报操作回调
  final Function(ComplainReason reason)? onComplain;

  const ComplainBottomSheet({super.key, this.onComplain});

  // 从 my-background.png 提取的颜色
  static const Color _primaryColor = Color(0xFF4A5568);
  static const Color _accentColor = Color(0xFFFFA680);

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final availableHeight = screenHeight * 0.8; // 可用高度为屏幕高度的80%

    return ConstrainedBox(
      constraints: BoxConstraints(maxHeight: availableHeight),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(24.r),
            topRight: Radius.circular(24.r),
          ),
          gradient: LinearGradient(
            begin: Alignment(1.00, 2.84),
            end: Alignment(0.00, 2.84),
            colors: [const Color(0xFFFFA680), const Color(0xFFFFEBE6)],
          ),
        ),
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 20.h),
          child: [
            // 拖拽指示器
            Container(
              margin: EdgeInsets.only(bottom: 4.h),
              width: 40.w,
              height: 4.h,
              decoration: BoxDecoration(
                color: Colors.grey[300],
                borderRadius: BorderRadius.circular(2.r),
              ),
            ),
            SizedBox(height: 4.h),
            // 举报选项列表
            ...ComplainReason.values.map(
              (reason) => _buildReasonItem(context, reason),
            ),
            SizedBox(height: 6.h),
            // 取消按钮
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12.r),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.05),
                    blurRadius: 8.w,
                    offset: Offset(0, 2.h),
                  ),
                ],
              ),
              child: ListTile(
                dense: true,
                contentPadding: EdgeInsets.symmetric(
                  horizontal: 16.w,
                  vertical: 4.h,
                ),
                title: Text(
                  'Cancel',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w500,
                    color: Colors.grey[700],
                  ),
                ),
                onTap: () => Navigator.of(context).pop(),
              ),
            ),
          ].verticalShot(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
          ),
        ),
      ),
    );
  }

  /// 构建举报原因选项
  Widget _buildReasonItem(BuildContext context, ComplainReason reason) {
    final isLast = reason == ComplainReason.values.last;
    return Container(
      margin: EdgeInsets.only(bottom: isLast ? 0 : 4.h),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8.w,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: ListTile(
        dense: true,
        contentPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
        leading: Container(
          width: 32.w,
          height: 32.w,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                _accentColor.withOpacity(0.2),
                _primaryColor.withOpacity(0.1),
              ],
            ),
          ),
          child: Icon(reason.icon, size: 16.w, color: _accentColor),
        ),
        title: Text(
          reason.label,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
            color: Colors.black87,
          ),
        ),
        trailing: Icon(
          Icons.chevron_right_rounded,
          color: Colors.grey[400],
          size: 18.w,
        ),
        onTap: () => _handleComplain(context, reason),
      ),
    );
  }

  /// 处理举报操作
  void _handleComplain(BuildContext context, ComplainReason reason) {
    Navigator.of(context).pop();

    // 执行传入的举报操作
    if (onComplain != null) {
      onComplain!(reason);
    } else {
      // 默认操作
      if (reason == ComplainReason.blockUser) {
        FlashNotifier.flashSuccess('User has been blocked');
      } else {
        FlashNotifier.flashSuccess('Report submitted: ${reason.label}');
      }
    }
  }
}

/// 显示举报底部表单的便捷方法
class ComplainHelper {
  /// 显示举报底部表单（曝光举报）
  /// [onComplain] 举报操作回调，用户选择举报原因后执行
  static Future<void> unveil(
    BuildContext context, {
    Function(ComplainReason reason)? onComplain,
  }) async {
    await showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      builder: (context) => ComplainBottomSheet(onComplain: onComplain),
    );
  }
}
